<?php
    /**
     * Created by PhpStorm.
     * 版权所有: 2019~2022 [ hhygyl ]
     * Date: 2019/8/21-9:03
     * Link: http://luckyadmin.luckyhhy.cn
     * FileName: Email.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */


    namespace app\admin\controller;


    use service\UtilService;
    use think\Exception;

    class Email extends SystemBase
    {

        protected $email;
        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub

            $this->email=new \app\admin\model\Email();
        }


        /**
         * @return mixed
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: index
         * @describe:获取分页数据
         */
        public function index(){

            if($this->request->isAjax()){
                $param=$this->request->param();
                return $this->email->GetAdminEmailDataPage($param);
            }

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: AddTemplate
         * @describe:添加模板内容
         */
        public function AddTemplate(){
            if($this->request->isAjax()){
                $data=$this->request->post();
                if(empty($data['content'])){
                    $this->error("模板内容不能为空");
                }
                $res=$this->email->addEditData($data);
                if($res['code']==1){
                    $this->success("操作成功");
                }else{
                    $this->error($res['msg']);
                }
            }
            return $this->fetch();
        }


        /**
         * @return mixed
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: EditTemplate
         * @describe: 编辑模板
         */
        public function EditTemplate(){
            $id=$this->request->param("id/d",0,"intval");
            $info=$this->email->get($id)->toArray();
            //dump($info);die;
            $this->assign("info",$info);
            return $this->fetch();
        }



        /**
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: delete
         * @describe:删除
         */
        public function delete()
        {
            if($this->request->isAjax()) {
                $ids = $this->request->post("ids");
                try {
                    $res = $this->email->del($ids); //模型删除
                    if($res) {
                        $this->success("删除成功");
                    }
                    else {
                        $this->error("删除失败");
                    }
                } catch (Exception $exception) {
                    $this->error($exception->getMessage());
                }
            }
        }



        /**
         * @throws \phpmailerException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: friendlink_email
         * @describe:
         */
        public function friendlink_email(){
            if($this->request->isAjax()){
                $data=$this->request->post();
                $res=$this->send_mail((int)$data['id'],$data['content']);
                if($res){
                    $this->success("发送成功");
                }else{
                    $this->error("发送失败");
                }
            }


        }


        /**
         * @param $id
         * @param $replay
         * @return bool
         * @throws \phpmailerException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: send_mail
         * @describe:友情链接回复
         */
        protected function send_mail($id,$replay){
            $yuan=db("friendlink")->find($id);
            $info=$this->email->get(['tag'=>'friend','status'=>1])->toArray();

            if(!$info){
                return false;
            }

            $arr=["username","content","replay","url","blog"];
            $need=[$yuan['email'],"博客名称：【".$yuan['name']."】<br/>网站地址：【".$yuan['href']."】",$replay,"<a href='http://www.jackhhy.cn/link/48.html' target='_blank'>链接</a>","<a href='http://www.jackhhy.cn' target='_blank'>小贺博客</a>"];
            foreach($arr as $k=>$v){
                $info['content']=str_replace($v,$need[$k],$info['content']);
            }
            $email=UtilService::is_email($yuan['email']);
            if(!$email){
                return false;
            }
            $ok=$this->Send_Maile("小贺个人博客友情链接申请回复提醒",$info['content'],$yuan['email']);
            if($ok){
                db("email_sends")->insert([
                    'email'=>$yuan['email'],
                    'title'=>"小贺个人博客友情链接申请回复提醒",
                    'content'=>$info['content'],
                    'nickname' =>$yuan['name'],
                    'ok'=>"发送成功",
                    'create_time'=>date("Y-m-d H:i:s",time())
                ]);
                return true;
            }else{
                return false;
            }
        }





    }